#include "WindowResizeble.h"
#include "Widgets/Mouse/MouseWidget.h"
#include "Widgets/WidgetTree.h"
#include "Widgets/Window.h"
RWindowResizeble::RWindowResizeble()
{

    auto OnBeginDrag=[this](){
        GetWindow()->StartCaptureMouse();

        InitialWindowPosition=GetWindow()->GetWindowPosition();
        InitialWindowSize=GetWindow()->GetWindowSize();
        InitialMouseScreenPosition=SMouseButtonEvent::GetMosuePositionOnScreen();
    };

    auto OnEndDrag=[this](){
        GetWindow()->StopCaptureMouse();
    };

    auto Top =NewWidget<RMouseWidget>(this);
    Top->Height=BorderSize;
    Top->Anchor.LeftReference=this;
    Top->Anchor.LeftOffset=BorderSize;
    Top->Anchor.RightReference=this;
    Top->Anchor.RightOffset=BorderSize;
    Top->CursorShape=EMouseCursorType::ResizeVertical;
    Top->Drag->bIsEnabled=true;
    Top->Drag->Target=(this);
    Top->Drag->Direction=EDragDirection::Customized;
    Top->Drag->OnDragStarted.Bind(OnBeginDrag);
    Top->Drag->OnDragMoved.Bind([this](const SVec2 & ){
        auto DeltaMouse=SMouseButtonEvent::GetMosuePositionOnScreen().Y-InitialMouseScreenPosition.Y;

        SIntPoint NewSize=SIntPoint(InitialWindowSize.X,InitialWindowSize.Y-DeltaMouse);
        GetWindow()->SetWindowSize(NewSize);

        SIntPoint NewPosition=SIntPoint(InitialWindowPosition.X,InitialWindowPosition.Y+DeltaMouse);
        GetWindow()->SetWindowPosition(NewPosition);
    });
    Top->Drag->OnDragEnded.Bind(OnEndDrag);

    auto Bottom =NewWidget<RMouseWidget>(this);
    Bottom->Height=BorderSize;
    Bottom->Anchor.LeftReference=this;
    Bottom->Anchor.LeftOffset=BorderSize;
    Bottom->Anchor.RightReference=this;
    Bottom->Anchor.RightOffset=BorderSize;
    Bottom->Anchor.BottomReference=this;
    Bottom->CursorShape=EMouseCursorType::ResizeVertical;
    Bottom->Drag->bIsEnabled=true;
    Bottom->Drag->Target=(this);
    Bottom->Drag->Direction=EDragDirection::Customized;
    Bottom->Drag->OnDragStarted.Bind(OnBeginDrag);
    Bottom->Drag->OnDragMoved.Bind([this](const SVec2 & ){
        auto DeltaMouse=SMouseButtonEvent::GetMosuePositionOnScreen()-InitialMouseScreenPosition;
        SIntPoint NewSize=SIntPoint(InitialWindowSize.X,InitialWindowSize.Y+DeltaMouse.Y);
        GetWindow()->SetWindowSize(NewSize);
    });
    Bottom->Drag->OnDragEnded.Bind(OnEndDrag);

    auto Left =NewWidget<RMouseWidget>(this);
    Left->Width=BorderSize;
    Left->Anchor.TopReference=this;
    Left->Anchor.TopOffset=BorderSize;
    Left->Anchor.BottomReference=this;
    Left->Anchor.BottomOffset=BorderSize;
    Left->CursorShape=EMouseCursorType::ResizeHorizontal;
    Left->Drag->bIsEnabled=true;
    Left->Drag->Target=(this);
    Left->Drag->Direction=EDragDirection::Customized;
    Left->Drag->OnDragStarted.Bind(OnBeginDrag);
    Left->Drag->OnDragMoved.Bind([this](const SVec2 & ){
        auto DeltaMouse=SMouseButtonEvent::GetMosuePositionOnScreen().X-InitialMouseScreenPosition.X;

        SIntPoint NewSize=SIntPoint(InitialWindowSize.X-DeltaMouse,InitialWindowSize.Y);
        GetWindow()->SetWindowSize(NewSize);

        SIntPoint NewPosition=SIntPoint(InitialWindowPosition.X+DeltaMouse,InitialWindowPosition.Y);
        GetWindow()->SetWindowPosition(NewPosition);
    });
    Left->Drag->OnDragEnded.Bind(OnEndDrag);

    auto Right =NewWidget<RMouseWidget>(this);
    Right->Width=BorderSize;
    Right->Anchor.TopReference=this;
    Right->Anchor.TopOffset=BorderSize;
    Right->Anchor.BottomReference=this;
    Right->Anchor.BottomOffset=BorderSize;
    Right->Anchor.RightReference=this;
    Right->CursorShape=EMouseCursorType::ResizeHorizontal;
    Right->Drag->bIsEnabled=true;
    Right->Drag->Target=(this);
    Right->Drag->Direction=EDragDirection::Customized;
    Right->Drag->OnDragStarted.Bind(OnBeginDrag);
    Right->Drag->OnDragMoved.Bind([this](const SVec2 & ){
        auto DeltaMouse=SMouseButtonEvent::GetMosuePositionOnScreen()-InitialMouseScreenPosition;
        SIntPoint NewSize=SIntPoint(InitialWindowSize.X+DeltaMouse.X,InitialWindowSize.Y);
        GetWindow()->SetWindowSize(NewSize);
    });
    Right->Drag->OnDragEnded.Bind(OnEndDrag);

    auto LeftTop =NewWidget<RMouseWidget>(this);
    LeftTop->Height=BorderSize;
    LeftTop->Width=BorderSize;
    LeftTop->CursorShape=EMouseCursorType::ResizeLeftTopTORightBottom;
    LeftTop->Drag->bIsEnabled=true;
    LeftTop->Drag->Target=(this);
    LeftTop->Drag->Direction=EDragDirection::Customized;
    LeftTop->Drag->OnDragStarted.Bind(OnBeginDrag);
    LeftTop->Drag->OnDragMoved.Bind([this](const SVec2 & ){
        auto DeltaMouse=SMouseButtonEvent::GetMosuePositionOnScreen()-InitialMouseScreenPosition;

        SIntPoint NewSize=SIntPoint(InitialWindowSize.X-DeltaMouse.X,InitialWindowSize.Y-DeltaMouse.Y);
        GetWindow()->SetWindowSize(NewSize);

        SIntPoint NewPosition=SIntPoint(InitialWindowPosition.X+DeltaMouse.X,InitialWindowPosition.Y+DeltaMouse.Y);
        GetWindow()->SetWindowPosition(NewPosition);
    });
    LeftTop->Drag->OnDragEnded.Bind(OnEndDrag);

    auto RightTop =NewWidget<RMouseWidget>(this);
    RightTop->Height=BorderSize;
    RightTop->Width=BorderSize;
    RightTop->Anchor.RightReference=this;
    RightTop->CursorShape=EMouseCursorType::ResizeRightTopTOLeftBottom;
    RightTop->Drag->bIsEnabled=true;
    RightTop->Drag->Target=(this);
    RightTop->Drag->Direction=EDragDirection::Customized;
    RightTop->Drag->OnDragStarted.Bind(OnBeginDrag);
    RightTop->Drag->OnDragMoved.Bind([this](const SVec2 & ){
        auto DeltaMouse=SMouseButtonEvent::GetMosuePositionOnScreen()-InitialMouseScreenPosition;

        SIntPoint NewSize=SIntPoint(InitialWindowSize.X+DeltaMouse.X,InitialWindowSize.Y-DeltaMouse.Y);
        GetWindow()->SetWindowSize(NewSize);

        SIntPoint NewPosition=SIntPoint(InitialWindowPosition.X,InitialWindowPosition.Y+DeltaMouse.Y);
        GetWindow()->SetWindowPosition(NewPosition);
    });
    RightTop->Drag->OnDragEnded.Bind(OnEndDrag);

    auto LeftBottom =NewWidget<RMouseWidget>(this);
    LeftBottom->Height=BorderSize;
    LeftBottom->Width=BorderSize;
    LeftBottom->Anchor.BottomReference=this;
    LeftBottom->CursorShape=EMouseCursorType::ResizeRightTopTOLeftBottom;
    LeftBottom->Drag->bIsEnabled=true;
    LeftBottom->Drag->Target=(this);
    LeftBottom->Drag->Direction=EDragDirection::Customized;
    LeftBottom->Drag->OnDragStarted.Bind(OnBeginDrag);
    LeftBottom->Drag->OnDragMoved.Bind([this](const SVec2 & ){
        auto DeltaMouse=SMouseButtonEvent::GetMosuePositionOnScreen()-InitialMouseScreenPosition;

        SIntPoint NewSize=SIntPoint(InitialWindowSize.X-DeltaMouse.X,InitialWindowSize.Y+DeltaMouse.Y);
        GetWindow()->SetWindowSize(NewSize);

        SIntPoint NewPosition=SIntPoint(InitialWindowPosition.X+DeltaMouse.X,InitialWindowPosition.Y);
        GetWindow()->SetWindowPosition(NewPosition);
    });
    LeftBottom->Drag->OnDragEnded.Bind(OnEndDrag);


    auto RightBottom =NewWidget<RMouseWidget>(this);
    RightBottom->Height=BorderSize;
    RightBottom->Width=BorderSize;
    RightBottom->Anchor.RightReference=this;   
    RightBottom->Anchor.BottomReference=this;
    RightBottom->CursorShape=EMouseCursorType::ResizeLeftTopTORightBottom;
    RightBottom->Drag->bIsEnabled=true;
    RightBottom->Drag->Target=(this);
    RightBottom->Drag->Direction=EDragDirection::Customized;
    RightBottom->Drag->OnDragStarted.Bind(OnBeginDrag);
    RightBottom->Drag->OnDragMoved.Bind([this](const SVec2 & ){
        auto DeltaMouse=SMouseButtonEvent::GetMosuePositionOnScreen()-InitialMouseScreenPosition;
        SIntPoint NewSize=SIntPoint(InitialWindowSize.X+DeltaMouse.X,InitialWindowSize.Y+DeltaMouse.Y);
        GetWindow()->SetWindowSize(NewSize);
    });
    RightBottom->Drag->OnDragEnded.Bind(OnEndDrag);
}